function TimeCode() {
  var date = new Date();
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
// 只获取 小时 分钟 秒
function TimeCodeFine() {
  var date = new Date();
  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()
  return [hour, minute, second].map(formatNumber).join(':')
}
//获取年月日时分秒
function formatTime(time, format) {
  const data = new Date(time)
  let year = data.getFullYear()
  // 由于 getMonth 返回值会比正常月份小 1
  let month = data.getMonth() + 1
  let day = data.getDate()
  let hours = data.getHours()
  let minutes = data.getMinutes()
  let seconds = data.getSeconds()
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds
}
// 通过时间戳来格式化日期，获得年月日时分秒星期
function dateFormat(time, format) {
  const data = new Date(time)
  let year = data.getFullYear()
  // 由于 getMonth 返回值会比正常月份小 1
  let month = data.getMonth() + 1
  let day = data.getDate()
  let hours = data.getHours()
  let minutes = data.getMinutes()
  let seconds = data.getSeconds()
  let week = data.getDay();
  let dayArr = [
    "星期日",
    "星期一",
    "星期二",
    "星期三",
    "星期四",
    "星期五",
    "星期六",
  ];
  return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + '-' + dayArr[week]
}
//日时分
function DayHourMinute(msTime) {
  let time = msTime / 1000;
  let day = Math.floor(time / 60 / 60 / 24);
  let hour = Math.floor(time / 60 / 60) % 24;
  let minute = Math.floor(time / 60) % 60;
  let second = Math.floor(time) % 60;
  return `${day}天${hour}时${minute}分${second}秒`
}

function TimeCodeYmd() {
  var date = new Date();
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  return [year, month, day].map(formatNumber).join('-');
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}
module.exports = {
  TimeCode,
  TimeCodeYmd,
  TimeCodeFine,
  formatTime,
  dateFormat,
  DayHourMinute
}